package com.liyao.storehouse.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;


public class ExcelUtil {
    public static void writeExcel(HttpServletResponse response, List<?> list, String title, Class<?> clazz) throws IOException {
        ExcelWriter excelWriter=EasyExcel.write(response.getOutputStream()).build();
        WriteSheet sheet=EasyExcel.writerSheet(0,title).head(clazz)
                .build();
        excelWriter.write(list,sheet);
        excelWriter.finish();
    }


    public static File transferToFile(MultipartFile multipartFile) {
//        选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。
        File file = null;
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String[] filename = originalFilename.split("\\.");
            file=File.createTempFile(filename[0], filename[1]);
            multipartFile.transferTo(file);
            file.deleteOnExit();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

}
